package com.kuang.demo.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kuang.demo.bo.zzy.AdminBo.*;
import com.kuang.demo.bo.zzy.AdminExample;
import com.kuang.demo.bo.zzy.TotalItem;
import com.kuang.demo.mapper.AdminMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {

    @Autowired
    AdminMapper adminMapper;

    @Override
    public TotalItem selectAllList(ListPageLimit listPageLimit) {
        //这里会预编译
        PageHelper.startPage(listPageLimit.getPage(),listPageLimit.getLimit());

        //这里是排序
        AdminExample adminExample = new AdminExample();
        adminExample.setOrderByClause(listPageLimit.getSort() + " " + listPageLimit.getOrder());
        AdminExample.Criteria criteria = adminExample.createCriteria();

        if(null != listPageLimit.getUsername())
        {
            criteria.andUsernameLike(listPageLimit.getUsername());
        }

        //不能搜寻逻辑删除的数据
        criteria.andDeletedEqualTo(false);

        List<IdNameAvaRole> list = adminMapper.selectByExample2(adminExample);
        TotalItem totalItem = new TotalItem();
        totalItem.setItems(list);

        PageInfo<IdNameAvaRole> idNameAvaRolePageInfo = new PageInfo<>(list);
        int total = (int) idNameAvaRolePageInfo.getTotal();
        totalItem.setTotal(total);

        return totalItem;
    }

    @Override
    public void deleteById(Integer id) {
        adminMapper.deleteById(id);
    }

    @Override
    public ReturnUpdate updateAdmin(IdNameAvaRole2 idNameAvaRole2) {
        adminMapper.updateAdmin(idNameAvaRole2);

        ReturnUpdate returnUpdate = adminMapper.selectUpdateAdmin(idNameAvaRole2.getId());
        return returnUpdate;
    }

    @Override
    public ReCreateAdmin CreateAdmin(ReturnUpdate returnUpdate) {
        adminMapper.createAdmin(returnUpdate);

        ReCreateAdmin reCreateAdmin = adminMapper.selectCreateAdmin(returnUpdate.getUsername());
        return reCreateAdmin;
    }

    @Override
    public int selectByUsername(String username) {

        int i = adminMapper.selectByUsername(username);
        return i;
    }
}
